#include "client.h"

int commandToken(char* buf, train_t* train){
    bzero(train,sizeof(train_t));
    char* saveptr;
    char* keyword = strtok_r(buf," ",&saveptr);
    if(strcmp(keyword,"cd") == 0){
        train->tag = CD;
    } 
    else if(strcmp(keyword,"ls") == 0){
        train->tag = LS;
    }
    else if(strcmp(keyword,"pwd") == 0){
        train->tag = PWD;
    }
    else if(strcmp(keyword,"mkdir") == 0){
        train->tag = MKDIR;
    }
    else if(strcmp(keyword,"rmdir") == 0){
        train->tag = RMDIR;
    }
    else if(strcmp(keyword,"puts") == 0){
        train->tag = PUTS;
    }
    else if(strcmp(keyword,"gets") == 0){
        train->tag = GETS;
    }
    else if(strcmp(keyword,"remove") == 0){
        train->tag = REMOVE;
    }
    else if(strcmp(keyword,"quit") == 0){
        train->tag = QUIT;
    }else{
        return -1;
    }
    char* path = strtok_r(NULL," ",&saveptr);
    if(path != NULL){
        train->length = strlen(path) + 1;
        memcpy(train->data, path, train->length);
    }else{
        train->length = 1;
        train->data[0] = '\0';
    }
    if(train->tag == LS || train->tag == PWD){
        if(strcmp(train->data,"") != 0){
            return -1;
        }
    }else {
        if(strtok_r(NULL," ",&saveptr) != NULL){
            return -1;
        }
    }
    return 0;
}
